/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * HoofdMenu.java
 *
 * Created on 1-jan-2010, 13:08:37
 */

package registratiesysteem;

import javax.swing.JDialog;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Scanner;
import java.io.FileReader;
import java.io.FileNotFoundException;
import javax.swing.JOptionPane;

/**
 *
 * @author Martin
 */
class Hoofdmenu extends javax.swing.JFrame {

    /** Creates new form HoofdMenu */
    Hoofdmenu() {
        super("Speel-o-theek Klapstuk");
        String host = null, database = null, user = null, pass = null;

        initComponents();
        setLocationRelativeTo(null);
        try {
            Scanner s = new Scanner(new FileReader("server.cfg"));
            if (s.hasNextLine()) {
                host = s.nextLine();
                if (s.hasNextLine()) {
                    database = s.nextLine();
                    if (s.hasNextLine()) {
                        user = s.nextLine();
                        if (s.hasNextLine()) {
                            pass = s.nextLine();
                            try {
                                beheerMenu = new BeheerMenu(this, new Database("jdbc:mysql://" + host + "/" + database, user, pass));
                                setVisible(true);
                            }
                            catch(RuntimeException ex) {
                                JOptionPane.showMessageDialog(null, ex.toString(), ex.getClass().getSimpleName(), JOptionPane.ERROR_MESSAGE);
                                new Login(this, host, database, user);
                            }
                        }
                        else
                            new Login(this, host, database, user);
                    }
                    else
                        new Login(this, host, database, user);
                }
                else
                    new Login(this, host, database, user);
            }
            else {
                JOptionPane.showMessageDialog(null, "MySQL server kan niet worden gevonden in server.conf", "MySQL server niet gevonden", JOptionPane.ERROR_MESSAGE);
                new Login(this, host, database, user);
            }
        }
        catch(FileNotFoundException e) {
            JOptionPane.showMessageDialog(null, e.toString(), "FileNotFoundException", JOptionPane.ERROR_MESSAGE);
            new Login(this, host, database, user);
        }
    }

    @Override
    public void dispose() {
        if (beheerMenu != null && beheerMenu.databaseConnectie != null)
            beheerMenu.databaseConnectie.close();
        super.dispose();
        System.exit(0);
    }

    class Login extends KeyAdapter {
        private JDialog login = new JDialog();
        private GridLayout layout = new GridLayout(0, 2);
        private JLabel jlHost = new JLabel("Server");
        private JLabel jlDatabase = new JLabel("Database");
        private JLabel jlUser = new JLabel("Gebruikersnaam");
        private JLabel jlPass = new JLabel("Wachtwoord");
        private JTextField jtfHost = new JTextField();
        private JTextField jtfDatabase = new JTextField();
        private JTextField jtfUser = new JTextField();
        private JPasswordField jpfPass = new JPasswordField();
        private JButton jbLogin = new JButton("Login");
        private JButton jbAnnuleren = new JButton("Annuleren");

        Login(final Hoofdmenu hoofdmenu, final String host, final String database, final String user) {
            layout.setHgap(5);
            layout.setVgap(5);
            login.setLayout(layout);
            jbLogin.setEnabled(false);
            jtfHost.setText(host);
            jtfDatabase.setText(database);
            jtfUser.setText(user);
            login.add(jlHost);
            login.add(jtfHost);
            login.add(jlDatabase);
            login.add(jtfDatabase);
            login.add(jlUser);
            login.add(jtfUser);
            login.add(jlPass);
            login.add(jpfPass);
            login.add(jbLogin);
            login.add(jbAnnuleren);
            login.setSize(275, 155);
            login.setResizable(false);
            login.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            login.setTitle("MySQL configuratie");
            login.setLocationRelativeTo(null);
            login.setVisible(true);
            jtfUser.addKeyListener(this);
            jpfPass.addKeyListener(this);
            jbLogin.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    try {
                        beheerMenu = new BeheerMenu(hoofdmenu, new Database("jdbc:mysql://" + jtfHost.getText() + "/" + jtfDatabase.getText(), jtfUser.getText(), new String(jpfPass.getPassword())));
                        login.dispose();
                        hoofdmenu.setVisible(true);
                    }
                    catch(RuntimeException ex) {
                        JOptionPane.showMessageDialog(null, ex.toString(), ex.getClass().getSimpleName(), JOptionPane.ERROR_MESSAGE);
                    }
                }
            });
            jbAnnuleren.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    login.dispose();
                    dispose();
                }
            });
        }

        @Override
        public void keyReleased(KeyEvent e) {
            if (!(jtfHost.getText().isEmpty() || jtfDatabase.getText().isEmpty() || jtfUser.getText().isEmpty() || jpfPass.getPassword().length == 0))
                jbLogin.setEnabled(true);
            else
                jbLogin.setEnabled(false);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Hoofdmenu();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton5 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jButton8 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButton6 = new javax.swing.JButton();

        jButton5.setText("jButton5");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("Beheer speelgoed");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Beheer leden");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Beheer uitleningen");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Toon overzichten");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton8.setText("Applicatie sluiten");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel1.setText("Registratiesysteem");

        jButton6.setText("Beheer reserveonderdelen");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jLabel1)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
                            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
                            .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE))
                        .addGap(10, 10, 10))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton8, javax.swing.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButton6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
                            .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addGap(18, 18, 18)
                .addComponent(jButton3)
                .addGap(18, 18, 18)
                .addComponent(jButton6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addComponent(jButton4)
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton8)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        beheerMenu.initialiseerBeheerMenu(BeheerObject.SPEELGOED);
        this.setVisible(false);
        beheerMenu.setLocationRelativeTo(null);
        beheerMenu.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        beheerMenu.initialiseerBeheerMenu(BeheerObject.LEDEN);
        this.setVisible(false);
        beheerMenu.setLocationRelativeTo(null);
        beheerMenu.setVisible(true);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        beheerMenu.initialiseerBeheerMenu(BeheerObject.UITLENINGEN);
        this.setVisible(false);
        beheerMenu.setLocationRelativeTo(null);
        beheerMenu.setVisible(true);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        beheerMenu.initialiseerBeheerMenu(BeheerObject.OVERZICHTEN);
        this.setVisible(false);
        beheerMenu.setLocationRelativeTo(null);
        beheerMenu.setVisible(true);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton8ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        beheerMenu.initialiseerBeheerMenu(BeheerObject.RESERVEONDERDELEN);
        this.setVisible(false);
        beheerMenu.setLocationRelativeTo(null);
        beheerMenu.setVisible(true);
    }//GEN-LAST:event_jButton6ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton8;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables

    /* Een instantie van het beheermenu waar men kan kiezen om een object
    toe te voegen, te wijzigen of te verwijderen */
    private BeheerMenu beheerMenu;

    // Enumeratie voor de verschillende functies van het BeheerMenu
    enum BeheerObject {
        SPEELGOED, LEDEN, UITLENINGEN, OVERZICHTEN, RESERVEONDERDELEN
    }
}
